<?php

	if(isset($_REQUEST["action"]))
	{

		$id=$_REQUEST["id"];
		$act=$_REQUEST["action"];

		$conn=mysql_connect('cmm.local','training','train@CMM') or die("server not connected");
		$db=mysql_select_db("training") or die("database not selected");


			if($act=="delete")
			{
				$qry="delete from shine_employ where id=$id";
				mysql_query($qry);
				header("Location:view.php");exit;
			}

			if($act=="edit")
			{
				if(isset($_POST["edit"]))
				{
					$nm=trim(addslashes(htmlspecialchars($_POST["nam"])));
					$ag=trim(addslashes(htmlspecialchars($_POST["age"])));
					$ocp=$_POST["Occupation"];
					$addr=trim(nl2br(addslashes(htmlspecialchars($_POST["Address"]))));
					$stat=$_POST["status"];
	
					$qry="update shine_employ set name='$nm',age='$ag',occupation='$ocp',status='$stat',address='$addr' where id='$id'";
	
					$res = mysql_query($qry);
					header("Location:view.php");exit;

				}
?>
<html>
	<head>
	<title>edit page</title>
	</head>

	<body>
	<div align="center">
		<h1>Employee Edit Page</h1>
		<form name="editfrm" action="#" method="post" onsubmit="return val()">
		<table border=1 align="center">
		<?php
			
			$edit_qry = "select * from shine_employ where id=$id";
			$res = mysql_query($edit_qry);
			$row = mysql_fetch_array($res);
			$arr = array("Doctor","Engineer","Teacher","Lawyer");
		?>
			
				<tr>
				<th>Name :</th>
				<td><input type="text" onblur="onchangecheck(this)" name="nam" id="nam" value="<?php echo $row['name']?>" maxlength=40></td>
				</tr>
				<tr>
				<th>Age :</th>
				<td><input type="text" onblur="onchangecheck(this)" name="age" id="age" value="<?php echo $row['age']?>" maxlength=3></td>
				</tr>
				<tr>
				<th>Occupation :</th>
				<td>
				<select name="Occupation" id="Occupation">
				<?php
					foreach($arr as $j)
						{	
							if($row["occupation"]==$j)
							{
								echo "<option value='$j' selected='selected'>$j</option>";
							}
							else
							{
								echo "<option value='$j'>$j</option>";
							}
						}
				?>
				</select>
				</td>
				</tr>
				<tr>
				<th style="vertical-align:top;">Address :</th>
				<td><textarea name="Address" id="Address" rows="5" cols="45"><?php echo $row['address']?></textarea></td>
				</tr>
				<tr>
				<th>Status :</th>
				<td>
					<?php 
						if($row["status"]=="active")
						{
					
							echo "<input type='radio' name='status' value='1' checked />Active";
							echo "<input type='radio' name='status' value='2'/>Inactive";
						}
						else
						{
							echo "<input type='radio' name='status' value='1'/>Active";
							echo "<input type='radio' name='status' value='2' checked />Inactive";
						}

						?>
				</td>
			</tr>
			<tr><td colspan=2 style="text-align:center;"><input type="submit" name="edit" value="save" /></td></tr>
		</table>
		</form>
		
	</div>
	</body>
</html>

<?php
	
				}

}//main if
?>


<style type="text/css">
	th
		{
			text-transform:uppercase;
			color:white;
			background-color:rgb(42,42,42);
			text-align:center;
			
		}

	table
		{
			/*border-collapse:collapse;*/
			border:2px solid rgb(42,42,42);
			outline:4px solid grey;
		}

	td
		{
			border:2px solid rgb(42,42,42);
			text-transform:capitalize;
		}




</style>

<script type="text/javascript">
function val()
			{
				error="";
				vals=true;

				if(document.editfrm['nam'].value=="")
					{error+="Enter Name \n"; vals=false;}

				
				
				if(document.editfrm['age'].value=="")
					{error+="Enter Age \n"; vals=false;}
								
				
				if(document.editfrm['Address'].value=="")
					{error+="Enter your address \n"; vals=false;}

				if(document.getElementsByName("status")[0].checked==false && document.getElementsByName("status")[1].checked==false)
				{
					error+="Select a state \n"; vals=false;
				}
				
				if(vals==false)
					{
						alert(error);
					}
				else
				{
					vals=true;
					error="";
				}
				return vals;
			}


			var pat1=/[a-z]/;
			var pat2=/[0-9]/;
function onchangecheck(el)
			{
				switch(el.name)
				{
					case "nam":
					
						if(document.editfrm['nam'].value!="" && !pat1.test(document.editfrm['nam'].value))
						{
						alert("Enter text for name field !");
						document.editfrm['nam'].style.color="red";
						document.editfrm['nam'].focus();
						//document.frm1['nam'].style.backgroundColor="blue";
						}
						else
						{
						document.editfrm['nam'].style.color="";
						}

						break;
					

					case "age":
					
						if(document.editfrm['age'].value!="" && !pat2.test(document.editfrm['age'].value))
						{
						alert("Enter number for age field !");
						document.editfrm['age'].style.color="red";
						document.editfrm['age'].focus();
						}
						else
						{
						document.editfrm['age'].style.color="";
						}
						if((document.editfrm['age'].value>100))
						{
							alert("age cant be greater than 100");
							document.editfrm['age'].value="";
							document.editfrm['age'].focus();
						}
						break;
					

					
				}

			}

</script>

